package com.mabs.service;

import java.util.List;

import com.mabs.dao.PersonDao;
import com.mabs.entity.Gender;
import com.mabs.entity.Person;
import com.mabs.util.BeanFactory;
import com.mabs.util.CheckUtil;
import com.mabs.util.InputUtil;

public class PersonService {
	static PersonDao personDao = (PersonDao) BeanFactory.getBean("personDao");

	public static void selectAllPersons() {
		List<Person> persons = personDao.selectAllPersons();
		System.out.println(" ------------------------------------------------------------------");
		System.out.printf("|  %-3s|  %-6s| %-7s|     %-10s|      %-8s|  %-8s|\n", "id", "name", "gender", "phone", "qq",
				"address");
		System.out.println(" ------------------------------------------------------------------");
		if (persons != null && persons.size() > 0) {
			for (Person p : persons) {
				System.out.printf("|  %-3d|  %-6s|   %-7s|  %-13s|  %-12s|  %-6s|\n", p.getId(), p.getName(),
						p.getGender().toString(), p.getPhone(), p.getQq(), p.getAddress());
				System.out.println(" ------------------------------------------------------------------");
			}
		} else {
			System.out.println("手机通讯录为空...");
		}

	}

	public static void deletePerson() {
		selectAllPersons();
		Long id = InputUtil.getLong("请输入编号:");
		List<Person> persons = personDao.selectAllPersons();
		Person person = null;
		for (Person p : persons) {
			if (p.getId() == id) {
				person = p;
			}
		}
		if (person == null) {
			System.out.println("输入错误...");
		} else {
			showInfo(person);
			String select = InputUtil.getInput("确认删除吗?(y/n):");
			if (select.equalsIgnoreCase("y")) {
				personDao.deletePersonById(id);
				System.out.println("用户已删除...");
			}
		}

	}

	public static void selectPersonById() {
		Long id = InputUtil.getLong("请输入编号:");
		List<Person> persons = personDao.selectAllPersons();
		Person person = null;
		for (Person p : persons) {
			if (p.getId() == id) {
				person = p;
			}
		}
		if (person == null) {
			System.out.println("查无此人...");
		} else {
			showInfo(person);
		}

	}

	public static void selectPersonByPhone() {
		String phone = InputUtil.getInput("请输入手机号:");
		List<Person> persons = personDao.selectAllPersons();
		Person person = null;
		for (Person p : persons) {
			if (p.getPhone().equals(phone)) {
				person = p;
			}
		}
		if (person == null) {
			System.out.println("查无此人...");
		} else {
			showInfo(person);
		}

	}

	public static void selectPersonByName() {
		String name = InputUtil.getInput("请输入姓名:");
		List<Person> persons = personDao.selectPersonByName(name);
		System.out.println(" ------------------------------------------------------------------");
		System.out.printf("|  %-3s|  %-6s| %-7s|     %-10s|      %-8s|  %-8s|\n", "id", "name", "gender", "phone", "qq",
				"address");
		System.out.println(" ------------------------------------------------------------------");
		if (persons != null && persons.size() > 0) {
			for (Person p : persons) {
				System.out.printf("|  %-3d|  %-6s|   %-7s|  %-13s|  %-12s|  %-5s|\n", p.getId(), p.getName(),
						p.getGender().toString(), p.getPhone(), p.getQq(), p.getAddress());
				System.out.println(" ------------------------------------------------------------------");
			}
		} else {
			System.out.println("查无此人...");
		}

	}

	public static void insertPerson() {
		List<Person> persons = personDao.selectAllPersons();
		Person person = null;
		System.out.println("增加通讯录");
		String name = InputUtil.getInput("请输入姓名:");
		String gender = InputUtil.getInput("请输入性别:");
		gender = CheckUtil.checkGender(gender);
		String phone = InputUtil.getInput("请输入手机号码:");
		phone = CheckUtil.checkPhone(phone, persons);
		String qq = InputUtil.getInput("请输入QQ号码:");
		qq = CheckUtil.checkQq(qq, persons);
		String address = InputUtil.getInput("请输入地址:");
		person = new Person(name, Gender.valueOf(gender.equals("男") ? "MALE" : "FEMALE"), phone, qq, address);
		personDao.insertPerson(person);
		System.out.println("添加成功...");
	}

	public static void updatePerson() {
		Long id = InputUtil.getLong("请输入编号:");
		List<Person> persons = personDao.selectAllPersons();
		Person person = null;
		for (Person p : persons) {
			if (p.getId() == id) {
				person = p;
			}
		}
		if (person == null) {
			System.out.println("查无此人...");
		} else {
			showInfo(person);
			String name = InputUtil.getInput("请输入新姓名:");
			String gender = InputUtil.getInput("请输入新性别:");
			gender = CheckUtil.checkGender(gender);
			String phone = InputUtil.getInput("请输入新手机号码:");
			phone = CheckUtil.checkPhone(phone, persons);
			String qq = InputUtil.getInput("请输入新QQ号码:");
			qq = CheckUtil.checkQq(qq, persons);
			String address = InputUtil.getInput("请输入新地址:");
			person = new Person(id, name, Gender.valueOf(gender.equals("男") ? "MALE" : "FEMALE"), phone, qq, address);
			personDao.updatePerson(person);
		}

	}

	public static void showInfo(Person person) {
		System.out.println("该编号对应的信息是:");
		System.out.println("姓名:" + person.getName());
		System.out.println("性别:" + person.getGender());
		System.out.println("手机号:" + person.getPhone());
		System.out.println("QQ号:" + person.getQq());
		System.out.println("地址:" + person.getAddress());
	}
}
